#include "CornProp.h"
#include "Tile.h"

CoinProp::CoinProp()
{
    timerJump.SetOneShot(true);
    timerJump.SetWaitTime(intervalJump);
    timerJump.SetOnTimeout([&]() { isJumping = false; });

    timerDisappear.SetOneShot(true);
    timerDisappear.SetWaitTime(intervalDisappear);
    timerDisappear.SetOnTimeout([&]() { isValid = false; });

    velocity.x = (rand() % 2 ? 1 : -1) * 2 * SIZE_TILE;
    velocity.y = -3 * SIZE_TILE;
}

void CoinProp::SetPosition(const Vector2 &position)
{
    this->position = position;
}

const Vector2 &CoinProp::GetPosition() const
{
    return position;
}

const Vector2 &CoinProp::GetSize() const
{
    return size;
}

void CoinProp::MakeInvalid()
{
    isValid = false;
}

bool CoinProp::CanRemove()
{
    return !isValid;
}

void CoinProp::OnUpdate(double delta)
{
    timerJump.OnUpdate(delta);
    timerDisappear.OnUpdate(delta);

    if (isJumping) {
        velocity.y += gravity * delta;
    } else {
        velocity.x = 0;
        velocity.y = sin(SDL_GetTicks64() / 1000.0 * 4) * 30; // 模拟有波动规律的移动
    }

    position += velocity * delta;
}

void CoinProp::OnRender(SDL_Renderer *renderer, SDL_Texture *tex)
{
    static SDL_Rect rect = {0, 0, (int) size.x, (int) size.y};
    rect.x = (int) (position.x - size.x / 2);
    rect.y = (int) (position.y - size.y / 2);
    SDL_RenderCopy(renderer, tex, nullptr, &rect);
}
